package com.fzhucc.modules.system.service.impl

import com.fzhucc.modules.system.domain.User
import com.fzhucc.ms.auth.domain.dto.UserDto
import com.fzhucc.ms.auth.domain.enums.AuthType
import com.fzhucc.ms.auth.service.UserService
import org.apache.dubbo.config.annotation.DubboReference
import org.springframework.stereotype.Service

/**
 * @author Woolsen
 * @since 2022/12/29 16:47
 */
@Service
class MsUserService {

    @DubboReference
    private lateinit var msUserService: UserService

    fun findByUsername(username: String): UserDto? {
        return msUserService.findByUsername(username)
    }

    fun create(user: User): UserDto {
        return msUserService.create(UserDto().apply {
            this.username = user.username
            this.password = user.password
            this.phone = user.phone
        })
    }

    fun update(user: User): UserDto {
        val dto = msUserService.findByUsername(user.username)
        return if (dto == null) {
            create(user)
        } else {
            dto.phone = user.phone
            msUserService.update(dto)
        }
    }

    @Throws(RuntimeException::class)
    fun login(username: String, password: String): UserDto {
        return msUserService.login(username, password)
    }

    @Throws(RuntimeException::class)
    fun oauth(type: AuthType, identifier: String): UserDto {
        return msUserService.oauth(type, identifier)
    }

    @Throws(RuntimeException::class)
    fun bind(userId: Long, type: AuthType, identifier: String) {
        msUserService.bindOAuth(userId, type, identifier)
    }

    @Throws(RuntimeException::class)
    fun unbind(userId: Long, type: AuthType) {
        msUserService.unbindOAuth(userId, type)
    }
}